<template>
  <el-dialog v-model="dialogFormVisible" :title="title" @close="closeDialog">
    <el-form
      :model="addBaseTeacherCarForm"
      ref="addBaseTeacherCarRef"
      :rules="addBaseTeacherCarrules"
    >

      <el-form-item
        label="人员姓名"
        :label-width="formLabelWidth"
        prop="teacherName"
      >
        <el-select
        v-model="addBaseTeacherCarForm.teacherName"
          filterable
          @change="filterData"
          placeholder="请选择人员姓名"
        >
          <el-option
            v-for="item in list"
            :key="item.teacherNumber"
            :label="item.name+'-'+item.teacherNumber"
            :value="item.name+'-'+item.teacherNumber"
          />
        </el-select>
      </el-form-item>
      <el-form-item
        label="车牌号"
        :label-width="formLabelWidth"
        prop="registerPlate"
      >
        <el-input
          v-model="addBaseTeacherCarForm.registerPlate"
          autocomplete="off"
          ref="name"
          placeholder="请填写车牌号"
        ></el-input>
      </el-form-item>
      <el-form-item
        label="考勤校区"
        :label-width="formLabelWidth"
        prop="registerPlate"
      >
      <el-radio-group v-model="addBaseTeacherCarForm.campus" size="large">
      <el-radio-button label="3" >考勤</el-radio-button>
      <el-radio-button label="4" >不考勤</el-radio-button>
    </el-radio-group>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="cancel()">取消</el-button>
        <el-button type="primary" @click="save()">保存</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script>
import { ref } from "vue";
import { httpService } from "@/utils/httpService";
import { ElMessage, ElForm } from "element-plus";
import { vxRule } from "@/utils/validator";

export default {
  name: "AddBaseTeacherCar",
  components: { ElForm, ElMessage },
  data() {
    return {
      dialogFormVisible: ref(false),
      formLabelWidth: "120px",
      title: "",
      personnelType: "",
      list:[],
      addBaseTeacherCarForm: {
        teacherName: "",
        teacherNumber: "",
        registerPlate: "",
        clientName: "",
        passTypeName: "",
        deadline: "",
        clientNo: "",
        createTime: "",
        lastUpdateTime: "",
        status: "",
        campus: 3,
      },
      // 校验规则
      addBaseTeacherCarrules: {
        teacherName: vxRule(true, null, "blur"),
        teacherNumber: vxRule(true, null, "blur"),
        registerPlate: vxRule(true, null, "blur"),
        clientName: vxRule(true, null, "blur"),
        passTypeName: vxRule(true, null, "blur"),
        deadline: vxRule(true, null, "blur"),
        clientNo: vxRule(true, null, "blur"),
      },
    };
  },
  methods: {
    closeDialog() {
      this.clear();
    },
    getBaseTeacherListApp() {
      httpService("/javaApi/baseTeacher/list/1/10000", {noCampus:9}, "get").then(
        (data) => {
          this.list = data.list;
          
        }
      );
    },
    cancel() {
      this.dialogFormVisible = false;
      this.clear();
    },
    getBaseTeacherCarById(id) {
      httpService("/javaApi/baseTeacherCar/queryById", { id: id }, "get").then(
        (data) => {
          this.addBaseTeacherCarForm = data;
        }
      );
    },
    clear() {
      this.dialogFormVisible = false;
      //清空form
      this.addBaseTeacherCarForm.id = "";
      this.addBaseTeacherCarForm.teacherName = "";
      this.addBaseTeacherCarForm.teacherNumber = "";
      this.addBaseTeacherCarForm.registerPlate = "";
      this.addBaseTeacherCarForm.clientName = "";
      this.addBaseTeacherCarForm.passTypeName = "";
      this.addBaseTeacherCarForm.deadline = "";
      this.addBaseTeacherCarForm.clientNo = "";
      this.addBaseTeacherCarForm.createTime = "";
      this.addBaseTeacherCarForm.lastUpdateTime = "";
      this.addBaseTeacherCarForm.status = "";
      this.addBaseTeacherCarForm.campus = "";
    },
    save() {
      this.$refs.addBaseTeacherCarRef.validate((valid) => {
        if (valid) {
          let params = new FormData(); //创建一个form对象,必须是form对象否则后端接受不到数据
          params.append("teacherName", this.addBaseTeacherCarForm.teacherName);
          let arr = this.addBaseTeacherCarForm.teacherName.split("-");
          params.append(
            "teacherNumber",
            arr[1]
          );
          params.append(
            "registerPlate",
            this.addBaseTeacherCarForm.registerPlate
          );
          params.append("clientName", this.addBaseTeacherCarForm.teacherName);
          params.append(
            "passTypeName",
            "固定车"
          );
          params.append("deadline", "2050-09-01");
          params.append("clientNo", this.addBaseTeacherCarForm.clientNo);
            params.append("campus", this.addBaseTeacherCarForm.campus);
          if (this.addBaseTeacherCarForm.id) {
            params.append("id", this.addBaseTeacherCarForm.id);
            httpService("/javaApi/baseTeacherCar/edit", params, "post").then(
              (data) => {
                if (data.code == 200) {
                  ElMessage.success("修改成功");
                  this.clear();
                  //刷新父组件的数据
                  this.$parent.getBaseTeacherCarList();
                } else {
                  ElMessage.error("修改失败");
                }
              }
            );
          } else {
            httpService("/javaApi/baseTeacherCar/add", params, "post").then(
              (data) => {
                if (data.code == 200) {
                  ElMessage.success("添加成功");
                  this.clear();
                  //刷新父组件的数据
                  this.$parent.getBaseTeacherCarList();
                } else {
                  ElMessage.error("添加失败");
                }
              }
            );
          }
        } else {
          return false;
        }
      });
    },
  },
};
</script>
<style>
.el-select{
    width: 100%;
}
</style>